package logic

import (
	"context"
	"srv/srv"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ReleaseLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewReleaseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReleaseLogic {
	return &ReleaseLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ReleaseLogic) Release(req *types.ReleaseRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	UserId := l.ctx.Value("userId").(uint)
	house, err := l.svcCtx.Srv.Release(l.ctx, &srv.ReleaseRequest{
		UserId:     int64(UserId),
		City:       req.City,
		Address:    req.Address,
		HouseCode:  req.HouseCode,
		HouseFloor: req.HouseFloor,
		HouseAcre:  req.HouseAcre,
		HousePrice: float32(req.HousePrice),
		Describe:   req.Describe,
	})

	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  err.Error(),
			Data: nil,
		}, nil

	}

	return &types.Response{
		Code: 200,
		Msg:  "房源发布成功",
		Data: map[string]interface{}{
			"HouseId": house.HouseId,
		},
	}, nil
}
